package com.jinxin.platform.cdsideparkmgr.service.impl;

import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdsideparkmgr.bean.CdyardmgrCarInfo;
import com.jinxin.platform.cdsideparkmgr.common.SysOrgInfo;
import com.jinxin.platform.cdsideparkmgr.mapper.CdsideparkmgrCarInfoMapper;
import com.jinxin.platform.cdsideparkmgr.rpc.CdsideparkmgrRpc;
import com.jinxin.platform.cdsideparkmgr.service.ICdsideparkmgrCarInfoService;
import com.jinxin.platform.cdsideparkmgr.utils.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-09-06
 * @Author: Lingnan
 **/
@Service
@Slf4j
public class CdsideparkmgrCarInfoServiceImpl implements ICdsideparkmgrCarInfoService {
    @Autowired
    CdsideparkmgrCarInfoMapper carInfoMapper;
    @Resource
    private IdWorker idWorker;
    @Autowired
    private CdsideparkmgrRpc cdsideparkmgrRpc;

    @Override
    public CdyardmgrCarInfo selectOne(HashMap param) {
        return carInfoMapper.selectOne(param);
    }

    @Override
    public List<CdyardmgrCarInfo> list(HashMap param) {
        return carInfoMapper.list(param);
    }

    @Override
    public List<CdyardmgrCarInfo> listPage(HashMap param) {
        return carInfoMapper.listPage(param);
    }

    @Override
    public int listPageCount(HashMap param) {
        return carInfoMapper.listPageCount(param);
    }

    @Override
    public JsonResult delete(String id) {
        if(StringUtils.isEmpty(id)){
            return JsonResult.error("Id不存在");
        }
        int result = carInfoMapper.deleteByKey(id);
        if(result > 0){
            return JsonResult.ok("删除成功","");
        }
        return JsonResult.error("删除失败");
    }

    @Override
    public JsonResult saveOrUpdate(CdyardmgrCarInfo record) {
        if (record == null){
            return JsonResult.error("参数错误，操作失败");
        }
        String id = record.getId();
        int count;
        String msg = "";
        if(StringUtils.isEmpty(id)){
            SysOrgInfo currentOrg = cdsideparkmgrRpc.getCurrentOrg();
            if(currentOrg == null){
                return JsonResult.error("获取组织信息失败");
            }
            String yardId = record.getYardId();
            String orgId = currentOrg.getOrgId();
            if(!StringUtils.isEmpty(yardId)){
                String province = record.getProvince();
                String carNo = record.getCarNo();
                HashMap param = new HashMap();
                param.put("yardId",yardId);
                param.put("province", province);
                param.put("carNo", carNo);
                param.put("orgId", orgId);
                CdyardmgrCarInfo cdyardmgrCarInfo = carInfoMapper.selectOne(param);
                if(cdyardmgrCarInfo != null){
                    String cph = province + carNo;
                    return JsonResult.error("车牌号("+cph+")已存在");
                }
            }
            record.setOrgId(orgId);
            record.setId(String.valueOf(idWorker.nextId()));
            count = carInfoMapper.insert(record);
            msg = "新增成功";
        }else {
            count = carInfoMapper.updateByKey(record);
            msg = "更新成功";
        }
        if(count > 0){
            return JsonResult.ok(msg,"");
        }else {
            return JsonResult.error("操作失败，请重试");
        }
    }

}
